/**
  ******************************************************************************
  * @file    COMM_DRIVER.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   ACAD Handling
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	COMMUNI_DRIVER_GLOBALS
#define	COMMUNI_DRIVER_GLOBALS
#include <includes.h>
#include "communi_driver.h"
#include "communi_server.h"
#include "app_usb.h"


static void COMMUNI_HWInit_RCC(void);
static void COMMUNI_HWInit_TIM(void);
static void COMMUNI_HWInit_NVIC(FunctionalState TIMCtrSta);


void COMMUNI_HWInit(void)
{
    COMMUNI_HWInit_RCC();
    COMMUNI_HWInit_TIM();
	COMMUNI_HWInit_NVIC(DISABLE);
	COMMUNI_HWInit_USB();
    RS232_Init();
    RS485_Init();
}


void COMMUNI_HWEnable(void)
{
    /* TIM enable/disable counter */
    TIM_Enable(CommuniTIM, ENABLE);
	COMMUNI_HWInit_NVIC(ENABLE);
}


static void COMMUNI_HWInit_TIM(void)
{
	OCInitType  TIM_OCInitStructure;
    TIM_TimeBaseInitType  TIM_TimeBaseStructure;
    
    /* TIM config */
    /* Choose Internal Clock */
    TIM_ConfigInternalClk(CommuniTIM);
    
    /* Time Base configuration */
    TIM_TimeBaseStructure.Prescaler     = CommuniTIM_Prescaler        ;
    TIM_TimeBaseStructure.CntMode       = CommuniTIM_CounterMode      ;
    TIM_TimeBaseStructure.Period        = CommuniTIM_Period           ;
    TIM_TimeBaseStructure.ClkDiv        = CommuniTIM_ClockDivision    ;
    TIM_TimeBaseStructure.RepetCnt      = CommuniTIM_RepetitionCounter;
    TIM_InitTimeBase(CommuniTIM, &TIM_TimeBaseStructure);
    
    /* Output Compare Active Mode configuration: Channel2 */
    TIM_OCInitStructure.OcMode       = USBTIM_OCMode      ;
    TIM_OCInitStructure.OutputState  = USBTIM_OutputState ;
    TIM_OCInitStructure.OutputNState = USBTIM_OutputNState;
    TIM_OCInitStructure.Pulse        = USBTIM_Pulse       ;
    TIM_OCInitStructure.OcPolarity   = USBTIM_OCPolarity  ;
    TIM_OCInitStructure.OcNPolarity  = USBTIM_OCNPolarity ;
    TIM_OCInitStructure.OcIdleState  = USBTIM_OCIdleState ;
    TIM_OCInitStructure.OcNIdleState = USBTIM_OCNIdleState;
    TIM_InitOc2(CommuniTIM, &TIM_OCInitStructure);
    /* Enable reload CR2 after EVENT */
    TIM_ConfigOc2Preload(CommuniTIM, TIM_OC_PRE_LOAD_DISABLE);

	/* Disable TIM counter */
    TIM_Enable(CommuniTIM, DISABLE);
    
    /* TIM Update Interrupt config */
    /* Clear TIM Update Flag */
    TIM_ClearFlag(CommuniTIM, CommuniTIM_FLAGx);
    /* Clear TIM Update Pending Bit */
    TIM_ClrIntPendingBit(CommuniTIM, CommuniTIM_ITx);
    
    TIM_ConfigInt(CommuniTIM, USBTIM_IT_CCx, DISABLE);
    TIM_ConfigInt(CommuniTIM, CommuniTIM_ITx  , DISABLE);
}


static void COMMUNI_HWInit_RCC(void)
{
  	RCC_EnableAPB1PeriphClk(CommuniTIM_RCCPeriph, ENABLE);

}

static void COMMUNI_HWInit_NVIC(FunctionalState TIMCtrSta)
{
    NVIC_InitType NVIC_InitStructure;
  
    /* TIM NVIC config */
    /* Enable the TIMx global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = CommuniTIM_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CommuniTIM_IRQPrePrio;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = CommuniTIM_IRQSubPrio;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = TIMCtrSta;
    NVIC_Init(&NVIC_InitStructure);
}


void TIM2_IRQHandler(void)
{ 
	uint16_t   CaptureValue;
	OS_CPU_SR  cpu_sr;
  
	OS_ENTER_CRITICAL();                         /* Tell uC/OS-II that we are starting an ISR          */
	OSIntEnter();
	OS_EXIT_CRITICAL();

	/* Timer Overflow */
	if(TIM_GetIntStatus(CommuniTIM, TIM_INT_UPDATE) == SET) 
	{
		/* Clear TIM2 Overflow interrupt pending bit */
		TIM_ClrIntPendingBit(CommuniTIM, TIM_INT_UPDATE);
	}

	/* OC2 Timer Overflow */
	if(TIM_GetIntStatus(CommuniTIM, TIM_INT_CC2) == SET) 
	{
		TIM_ClrIntPendingBit(CommuniTIM, TIM_INT_CC2);
		TIM_ConfigInt(CommuniTIM, TIM_INT_CC2, DISABLE);
		
		USBRXBufferCount=0;
	} 
	
	OSIntExit();                                 /* Tell uC/OS-II that we are leaving the ISR          */
}


#endif	/*	COMMUNI_DRIVER_GLOBALS	*/